<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Menashop
 * @package    Menashop_Content
 * @subpackage CategoryFeatures
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The SAX handler for category features
 * 
 * @category   Menashop
 * @package    Menashop_Content
 * @subpackage CategorieFeatures
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Menashop_Content_CategoryFeatures
{

    /**
     * The data model for category
     * @var Admin_Model_Category
     */
    protected $_categoryModel;

    /**
     * The date model for feature
     * @var Admin_Model_Feature
     */
    protected $_featureModel;

    /**
     * The data model for feature group
     * @var Admin_Model_FeatureGroup
     */
    protected $_featureGroupModel;

    /**
     * The data model for feature group translation
     * @var Admin_Model_FeatureGroupTranslation
     */
    protected $_featureGroupTranslationModel;

    /**
     * The data model for category feature group
     * @var Admin_Model_CategoryFeatureGroup 
     */
    protected $_categoryFeatureGroupModel;

    /**
     * The data model for category feature
     * @var Admin_Model_CategoryFeature
     */
    protected $_categoryFeatureModel;

    /**
     * The languages
     * @var array 
     */
    protected $_languages;

    /**
     * The category ID
     * @var integer
     */
    protected $_categoryId;

    /**
     * Feature group
     * @var Admin_Model_FeatureGroup 
     */
    protected $_featureGroup;

    /**
     * Feature group translations 
     * @var array 
     */
    protected $_featureGroupTranslations;

    /**
     * Category feature group
     * @var Admin_Model_CategoryFeatureGroup
     */
    protected $_categoryFeatureGroup;

    /**
     * Category feature
     * @var Admin_Model_CategoryFeature 
     */
    protected $_categoryFeature;

    /**
     * The flag for a tag 'FeatureGroup'
     * @var array 
     */
    protected $_inFeatureGroup = 0;

    /**
     * The hash for processed feature groups
     * @var array 
     */
    protected $_processedFeatureGroups = array();

    /**
     * The hash for processed features
     * @var array 
     */
    protected $_processedFeatures = array();

    /**
     * The hash for processed category feature groups
     * @var array 
     */
    protected $_processedCategoryFeatureGroups = array();

    /**
     * The counter for feature groups
     * @var ineteger 
     */
    protected $_totalFeatureGroups = 0;

    /**
     * The counter for inserted feature groups
     * @var integer
     */
    protected $_totalFeatureGroupsInserted = 0;

    /**
     * The counter for updated feature groups
     * @var integer
     */
    protected $_totalFeatureGroupsUpdated = 0;

    /**
     * The counter for feature group translations
     * @var ineteger 
     */
    protected $_totalFeatureGroupTranslations = 0;

    /**
     * The counter for inserted feature group translations
     * @var integer
     */
    protected $_totalFeatureGroupTranslationsInserted = 0;

    /**
     * The counter for updated feature group translations
     * @var integer
     */
    protected $_totalFeatureGroupTranslationsUpdated = 0;

    /**
     * The counter for category feature groups
     * @var integer
     */
    protected $_totalCategoryFeatureGroups = 0;

    /**
     * The counter for inserted category feature groups
     * @var integer 
     */
    protected $_totalCategoryFeatureGroupsInserted = 0;

    /**
     * The counter for updated category feature groups
     * @var integer 
     */
    protected $_totalCategoryFeatureGroupsUpdated = 0;

    /**
     * The counter for category features
     * @var integer 
     */
    protected $_totalCategoryFeatures = 0;

    /**
     * The counter for inserted category features
     * @var integer 
     */
    protected $_totalCategoryFeaturesInserted = 0;

    /**
     * The counter for updated category features
     * @var integer 
     */
    protected $_totalCategoryFeaturesUpdated = 0;

    /**
     * Initialize the SAX handler for category features
     * @param array $languages 
     */
    public function __construct($languages)
    {
        $this->_categoryModel = new Admin_Model_Category();
        $this->_featureModel = new Admin_Model_Feature();
        $this->_featureGroupModel = new Admin_Model_FeatureGroup();
        $this->_featureGroupTranslationModel = new Admin_Model_FeatureGroupTranslation();
        $this->_categoryFeatureGroupModel = new Admin_Model_CategoryFeatureGroup();
        $this->_categoryFeatureModel = new Admin_Model_CategoryFeature();
        $this->_languages = $languages;
    }

    /**
     * Process the start of an element in XML
     * @param obejct $parser
     * @param string $tag
     * @param array $attributes 
     */
    public function startElement($parser, $tag, $attributes)
    {
        if ('Category' == $tag) {
            // Find a category in the database
            $categoryId = $this->_categoryModel->findIdByContentId($attributes['ID']);
            if ($categoryId) {
                $this->_categoryId = $categoryId;
            }
        } elseif ('CategoryFeatureGroup' == $tag) {
            $this->_categoryFeatureGroup = array();
            // check an empty category feature group
            if ($attributes) {
                $this->_categoryFeatureGroup['content_id'] = $attributes['ID'];
                $this->_categoryFeatureGroup['no'] = $attributes['No'];
            }
        } elseif ('FeatureGroup' == $tag) {
            $this->_featureGroup = array();
            $this->_featureGroup['content_id'] = $attributes['ID'];
            $this->_inFeatureGroup = 1;
            $this->_featureGroupsTranslations = array();
        } elseif ('Name' == $tag) {
            // check a feature group translation for the current language 
            if ($this->_inFeatureGroup && in_array($attributes['langid'], $this->_languages)) {
                $featureGroupTranslation = null;
                // check a feature group translation
                if (isset($this->_featureGroupsTranslations[$attributes['langid']])) {
                    $featureGroupTranslation = &$this->_featureGroupTranslations[$attributes['langid']];
                } else {
                    // create a new feature group translation
                    $featureGroupTranslation = array();
                    $featureGroupTranslation['language_id'] = $attributes['langid'];
                    $this->_featureGroupTranslations[$attributes['langid']] = &$featureGroupTranslation;
                }
                $featureGroupTranslation['name'] = $attributes['Value'];
            }
        } elseif ('Feature' == $tag) {
            $this->_categoryFeature = array();
            // try to get ID of a category feature group from the hash
            if (isset($this->_processedCategoryFeatureGroups[$attributes['CategoryFeatureGroup_ID']])) {
                $this->_categoryFeature['category_feature_group_id'] = $this->_processedCategoryFeatureGroups[$attributes['CategoryFeatureGroup_ID']];
            }
            // try to get ID of a feature from the hash
            if (isset($this->_processedFeatures[$attributes['ID']])) {
                $this->_categoryFeature['feature_id'] = $this->_processedFeatures[$attributes['ID']];
            } else {
                // find a feature in the database
                $featureId = $this->_featureModel->findIdByContentId($attributes['ID']);
                if ($featureId) {
                    $this->_categoryFeature['feature_id'] = $featureId;
                    $this->_processedFeatures[$attributes['ID']] = $featureId;
                }
            }
            $this->_categoryFeature['content_id'] = $attributes['CategoryFeature_ID'];
            $this->_categoryFeature['mandatory'] = $attributes['Mandatory'];
            $this->_categoryFeature['dropdown_input'] = $attributes['Use_Dropdown_Input'];
            $this->_categoryFeature['no'] = $attributes['No'];
            $this->_categoryFeature['searchable'] = $attributes['Searchable'];
        }
    }

    /**
     * Process the end of an element in XML
     * @param object $parser
     * @param string $tag 
     */
    public function endElement($parser, $tag)
    {
        if ('FeatureGroup' == $tag) {
            // try to get a feature group from the hash
            if (isset($this->_processedFeatureGroups[$this->_featureGroup['content_id']])) {
                $this->_featureGroup['id'] = $this->_processedFeatureGroups[$this->_featureGroup['content_id']];
            } else {
                // process a feature group
                $featureGroupContentId = $this->_featureGroup['content_id'];
                // find a feature group in the database
                $featureGroupId = $this->_featureGroupModel->findIdByContentId($featureGroupContentId);
                if (!$featureGroupId) {
                    // insert the feature group
                    $featureGroupId = $this->_featureGroupModel->save($this->_featureGroup);
                    $this->_totalFeatureGroupsInserted++;
                } else {
                    $this->_totalFeatureGroupsUpdated++;
                }
                $this->_featureGroup['id'] = $featureGroupId;
                $this->_processedFeatureGroups[$featureGroupContentId] = $featureGroupId;
                $this->_totalFeatureGroups++;
                foreach ($this->_featureGroupTranslations as $languageId => $featureGroupTranslation) {
                    $featureGroupTranslationId = $this->_featureGroupTranslationModel->findIdByFeatureGroupAndLanguage($featureGroupId, $languageId);
                    if ($featureGroupTranslationId) {
                        // update the category translation
                        $featureGroupTranslation['id'] = $featureGroupTranslationId;
                        $this->_totalFeatureGroupTranslationsUpdated++;
                    } else {
                        // insert the category translation
                        $this->_totalFeatureGroupTranslationsInserted++;
                    }
                    $featureGroupTranslation['feature_group_id'] = $featureGroupId;
                    $this->_featureGroupTranslationModel->save($featureGroupTranslation);
                    $this->_totalFeatureGroupTranslations++;
                }
            }
            $this->_inFeatureGroup = 0;
        } elseif ('CategoryFeatureGroup' == $tag) {
            if (isset($this->_categoryFeatureGroup['content_id'])) {
                if ($this->_categoryId) {
                    $this->_categoryFeatureGroup['category_id'] = $this->_categoryId;
                }
                $this->_categoryFeatureGroup['feature_group_id'] = $this->_featureGroup['id'];
                // find a category feature group in the database
                $categoryFeatureGroupContentId = $this->_categoryFeatureGroup['content_id'];
                $categoryFeatureGroupId = $this->_categoryFeatureGroupModel->findIdByContentId($categoryFeatureGroupContentId);
                if ($categoryFeatureGroupId) {
                    // update the category feature group
                    $this->_categoryFeatureGroup['id'] = $categoryFeatureGroupId;
                    $this->_totalCategoryFeatureGroupsUpdated++;
                } else {
                    // insert the category feature group
                    $this->_totalCategoryFeatureGroupsInserted++;
                }

                $categoryFeatureGroupId = $this->_categoryFeatureGroupModel->save($this->_categoryFeatureGroup);
                // add a processed category feature group to the hash
                $this->_processedCategoryFeatureGroups[$categoryFeatureGroupContentId] = $categoryFeatureGroupId;
            }
            $this->_totalCategoryFeatureGroups++;
        } elseif ('Feature' == $tag) {
            // find a category feature in database
            $categoryFeatureId = $this->_categoryFeatureModel->findIdByContentId($this->_categoryFeature['content_id']);
            if ($categoryFeatureId) {
                // update the category feature
                $this->_categoryFeature['id'] = $categoryFeatureId;
                $this->_totalCategoryFeaturesUpdated++;
            } else {
                // insert the category feature
                $this->_totalCategoryFeaturesInserted++;
            }
            $this->_categoryFeatureModel->save($this->_categoryFeature);
            $this->_totalCategoryFeatures++;
        }
    }

    /**
     * Process characters in XML
     * @param object $parser
     * @param string $data 
     */
    public function characterData($parser, $data)
    {
        
    }

    /**
     * Get the report about a parsing
     * @return string 
     */
    public function getReport()
    {
        $report = "$this->_totalFeatureGroups feature groups in XML,";
        $report .= " $this->_totalFeatureGroupsInserted inserted,";
        $report .= " $this->_totalFeatureGroupsUpdated updated\n";
        $report .= "$this->_totalFeatureGroupTranslations feature group translations in XML,";
        $report .= " $this->_totalFeatureGroupTranslationsInserted inserted,";
        $report .= " $this->_totalFeatureGroupTranslationsUpdated updated\n";
        $report .= "$this->_totalCategoryFeatureGroups category feature groups in XML,";
        $report .= " $this->_totalCategoryFeatureGroupsInserted inserted,";
        $report .= " $this->_totalCategoryFeatureGroupsUpdated updated\n";
        $report .= "$this->_totalCategoryFeatures category features in XML,";
        $report .= " $this->_totalCategoryFeaturesInserted inserted,";
        $report .= " $this->_totalCategoryFeaturesUpdated updated";
        return $report;
    }

}

